/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PuestoMod.java
 *
 * Created on 06-jul-2011, 16:16:13
 */
package labhibernate;

import Entity.AcPuesto;
import Hibernate_.HibernateUtil;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Fam
 */
public class PuestoMod extends javax.swing.JFrame {

    /** Creates new form PuestoMod */
    public PuestoMod() {
        initComponents();
        GetNames();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        pNombre = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        pDesc = new javax.swing.JTextArea();
        pSueldo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jNombres = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(191, 228, 246));

        jLabel1.setFont(new java.awt.Font("Maiandra GD", 1, 18)); // NOI18N
        jLabel1.setText("MODIFICAR PUESTO");

        jLabel2.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        jLabel2.setText("NOMBRE");

        jLabel3.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        jLabel3.setText("DESCRIPCIÓN");

        jLabel4.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        jLabel4.setText("SUELDO BASE");

        pNombre.setFont(new java.awt.Font("Maiandra GD", 0, 12));

        pDesc.setColumns(20);
        pDesc.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        pDesc.setRows(5);
        jScrollPane1.setViewportView(pDesc);

        pSueldo.setFont(new java.awt.Font("Maiandra GD", 0, 12));

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/depto_Obligacion.jpg"))); // NOI18N
        jLabel5.setText("jLabel5");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/4158.png"))); // NOI18N
        jButton1.setText("CANCELAR");

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/4149.png"))); // NOI18N
        jButton2.setText("ACEPTAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jNombres.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jNombres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNombresActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(jLabel1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(jNombres, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(27, 27, 27))
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(jLabel3))
                        .addGap(22, 22, 22)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pSueldo)
                            .addComponent(pNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jButton1)
                        .addGap(51, 51, 51)
                        .addComponent(jButton2)))
                .addContainerGap(61, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pSueldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(89, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        float sueldo = new Float(pSueldo.getText());
        CURD puesto = new CURD();
        puesto.UpdatePuesto(pNombre.getText(), pDesc.getText(), sueldo);
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jNombresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNombresActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jNombresActionPerformed

     public void GetNames(){
        
        List resultList = null;
        
                try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query q = session.createQuery("from AcPuesto");
        
        if( q.list().isEmpty()){
            JOptionPane.showMessageDialog(null,"No hay registros que coincidan");
        }
        else{
            resultList = q.list();
            session.getTransaction().commit();
        }
        
    } catch (HibernateException he) {
        he.printStackTrace();
    }        
        
       Vector tableData = new Vector();
        for(Object o : resultList) {
        
        AcPuesto puesto = new AcPuesto((AcPuesto)o);
        Vector<Object> oneRow = new Vector<Object>();
        Vector<Object> cr = new Vector<Object>();
        oneRow.add(puesto.getNombrePuesto() );
        tableData.add(oneRow);
        
        
        
    }
       jNombres.setModel(new DefaultComboBoxModel(tableData));
      
               
          
        
    }
    
    
     public void SetPuesto(String data, int op){
        List resultList = null;
        String query = new String();
         query = "select e from AcEmpleado e where e.carneEmpleado= '"+data+"'";
        try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query q = session.createQuery("select e from AcPuesto p where p.nombrePuesto= '"+data+"'");
        JOptionPane.showMessageDialog(null, String.valueOf(q.list()));
       
        if( q.list().isEmpty()){
           JOptionPane.showMessageDialog(null,"No hay registros que coincidan");
        }
               
       else{
            resultList = q.list();
            session.getTransaction().commit();
       
            
             Vector tableData = new Vector();
       for(Object o : resultList) {
        
        AcPuesto puesto = new AcPuesto((AcPuesto)o);
        
        pNombre.setText(puesto.getNombrePuesto());
        pDesc.setText(puesto.getDescripcion());
        pSueldo.setText(String.valueOf(puesto.getSueldo()));
            
          
        }
        }
        
        }  
     catch (HibernateException he) {
        he.printStackTrace();
    }        
     }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PuestoMod().setVisible(true);
            }
        });
    }
    
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox jNombres;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea pDesc;
    private javax.swing.JTextField pNombre;
    private javax.swing.JTextField pSueldo;
    // End of variables declaration//GEN-END:variables
}
